import java.util.Arrays;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: mirac
 * Date: 2025-08-12
 * Time: 22:50
 */
public class MinStack {
    int[] val;
    int size = 0;

    int[] arr;
    //初始化堆栈对象。
    public MinStack() {
        this.val = new int[10];
        this.arr = new int[10];
    }

    //将元素val推入堆栈。
    public void push(int val) {
        if (isFull()) {
            //满了进行扩容
            this.val = Arrays.copyOf(this.val,this.val.length * 2);
            this.arr = Arrays.copyOf(this.arr,this.arr.length * 2);
        }
        this.val[this.size] = val;
        this.arr[this.size] = val;
        this.size++;
        sort(arr);
    }
    //判断栈满
    private boolean isFull() {
        return this.size == val.length;
    }

    //删除堆栈顶部的元素。
    public void pop() {
        this.size--;
        sort(this.arr);
    }

    //获取堆栈顶部的元素。
    public int top() {
        return this.val[this.size - 1];
    }

    //获取堆栈中的最小元素。
    public int getMin() {
        return this.arr[this.size - 1];
    }

    //对arr数组进行排序
    private void sort(int[] arr) {
        for (int i = 0; i < this.size; i++) {
            for (int j = 0; j < this.size - 1 - i; j++) {
                if (arr[j] < arr[j + 1]) {
                    int ans = arr[j];
                    arr[j] = arr[j + 1];
                    arr[j + 1] = ans;
                }
             }
        }
    }
}
